package mapper;

import com.javabean.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.ArrayList;

public interface RoleMapper {

    @Select("select * from role")
    ArrayList<Role> selectAll();

    @Delete("delete from role where rid=#{rid}")
    int deleteByRid(int rid);

    @Insert("insert into role(name,`desc`)values(#{name},#{desc})")
    int insertRole(Role role);

    //在传递多个参数时 默认使用arg0.. param1.. 代表参数
    //注解开发可以使用注解绑定参数 来指定参数的名称
    @Update("update role set name=#{name} where rid=#{rid}")
    int updateNameByRid(@Param("rid") int rid, @Param("name") String name);


    @Select("select ur.uid,r.* from role r,user_role ur where r.rid=ur.rid and uid=#{uid}")
    @ResultMap("r_p")
    Role SelectByUid(int uid);


    @Select("select * from role")
    @Results(id = "r_p",value = {
            @Result(id = true,property = "rid",column = "rid"),
            @Result(property = "name",column = "name"),
            @Result(property = "desc",column = "desc"),
            @Result(property = "plist",column = "rid",many = @Many(
                    select = "mapper.permissionMapper.selectByRid",
                    fetchType = FetchType.LAZY
            ))
    })
    ArrayList<Role> selectR_P();

}
